package com.cn.hnust.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.hnust.pojo.Customer;
import com.cn.hnust.pojo.Lfdappoint;
import com.cn.hnust.service.ICustomerService;
import com.cn.hnust.service.ILfdappointService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/Lfdappoint")
public class LfdappointController {

	@Resource
	private ILfdappointService LfdappointService;

	@Autowired
	private ICustomerService customerService;

	@RequestMapping("/selectLfdappointByOpenid")
	public void selectLfdappointByOpenid(HttpServletResponse response, Lfdappoint Lfdappoint, String openid)
			throws IOException {
		List<Lfdappoint> list = LfdappointService.selectLfdappointByOpenid(openid);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/selectLfdappointByLid")
	public void selectLfdappointByLid(HttpServletResponse response, Lfdappoint Lfdappoint, Integer lid)
			throws IOException {
		List<Lfdappoint> list = LfdappointService.selectLfdappointByLid(lid);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/addLfdappoint")
	public void addLfdappoint(HttpServletResponse response, Lfdappoint Lfdappoint, Customer Customer, Customer cus,
			String openid, String beizhu, String dianhua, String riqi, Integer lid, Integer sid, String xingbie,
			String xingming, String yuefen, Integer money) throws IOException {
		Lfdappoint.setBeizhu(beizhu);
		Lfdappoint.setDianhua(dianhua);
		Lfdappoint.setLid(lid);
		Lfdappoint.setOpenid(openid);
		Lfdappoint.setRiqi(riqi);
		Lfdappoint.setSid(sid);
		Lfdappoint.setXingbie(xingbie);
		Lfdappoint.setXingming(xingming);
		Lfdappoint.setMoney(money);
		Lfdappoint.setYuefen(yuefen);
		LfdappointService.insertSelective(Lfdappoint);
		cus = customerService.selectCustomerByOpenid(openid);
		int zmoney = cus.getMoney() - money;
		Customer.setId(cus.getId());
		Customer.setMoney(zmoney);
		customerService.updateByPrimaryKeySelective(Customer);
		List<Lfdappoint> list = LfdappointService.selectLfdappointByOpenid(openid);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/updateLfdappoint")
	public void updateLfdappoint(HttpServletResponse response, Lfdappoint Lfdappoint, Integer id, Integer status)
			throws IOException {
		System.out.println("id:"+id+"status:"+status);
		Lfdappoint.setStatus(status);
		Lfdappoint.setId(id);
		LfdappointService.updateByPrimaryKeySelective(Lfdappoint);
		Gson gson = new Gson();
		String json = gson.toJson("");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
}
